<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Remapping Keys and Buttons</title>
<meta name="description" content="Free keyboard remapper that can also remap mouse and joystick buttons. It can also automate repetitive tasks by sending keystrokes &amp; mouse clicks.">
<meta name="keywords" content="keyboard,remapper,remap,remapping,keys,key,keystrokes,clicks,mouse,buttons,button,joystick,hotkeys,hotkey">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>Remapping Keys and Buttons</h1>

<h2>Introduction</h2>
<p><strong>Limitation</strong>: AutoHotkey's remapping feature described below is generally not as pure and effective as remapping directly via the Windows registry. For the advantages and disadvantages of each approach, see <a href="#registry">registry remapping</a>.</p>
<h2 id="Remap">Remapping the Keyboard and Mouse</h2>
<p>The syntax for the built-in remapping feature is <code>OriginKey::DestinationKey</code>. For example, a <a href="../Scripts.htm">script</a> consisting only of the following line would make the &quot;a&quot; key behave like the &quot;b&quot; key:</p>
<pre>a::b</pre>
<p>The above example does not alter the &quot;b&quot; key itself. The &quot;b&quot; key would continue to send the &quot;b&quot; keystroke unless you remap it to something else as shown in the following example:</p>
<pre>a::b
b::a</pre>
<p>The examples above use lowercase, which is recommended for most purposes because it also remaps the corresponding uppercase letters (that is, it will send uppercase when Capslock is &quot;on&quot; or the Shift key is held down). By contrast, specifying an uppercase letter on the right side forces uppercase. For example, the following line would produce an uppercase B when you type either &quot;a&quot; or &quot;A&quot; (as long as Capslock is off):</p>
<pre>a::B</pre>
<p>&nbsp;</p>
<p><strong><a name="RemapMouse"></a>Mouse remapping</strong>: To remap the mouse instead of the keyboard, use the same approach. For example:</p>
<table class="info">
  <tr>
    <td>MButton::Shift</td>
    <td>Makes the middle button behave like the Shift key.</td>
  </tr>
  <tr>
    <td>XButton1::LButton</td>
    <td>Makes the fourth mouse button behave like the left mouse button.</td>
  </tr>
  <tr>
    <td>RAlt::RButton</td>
    <td>Makes the right Alt key behave like the right mouse button.</td>
  </tr>
</table>
<p>&nbsp;</p>
<p><strong>Other useful remappings:</strong></p>
<table class="info">
  <tr>
    <td>Capslock::Ctrl</td>
    <td>Makes Capslock become a Control key. To retain the ability to turn Capslock on and off, add the remapping <code>+Capslock::Capslock</code> first. This toggles Capslock on and off when you hold down the Shift key and press Capslock. Because both remappings allow additional modifier keys to be held down, the more specific <code>+Capslock::Capslock</code> remapping must be placed first for it to work.</td>
  </tr>
  <tr>
    <td>XButton2::^LButton</td>
    <td>Makes the fifth mouse button (XButton2) produce Control-LeftClick.</td>
  </tr>
  <tr>
    <td>RAlt::AppsKey</td>
    <td>Makes the right Alt key become the Apps key (which is the key that opens the context menu).</td>
  </tr>
  <tr>
    <td>RCtrl::RWin</td>
    <td>Makes the right Control key become the right Windows key.</td>
  </tr>
  <tr>
    <td>Ctrl::Alt</td>
    <td>Makes both Control keys behave like an Alt key. However, see <a href="#AltTab">alt-tab issues</a>.</td>
  </tr>
  <tr>
    <td>^x::^c</td>
    <td>Makes Control-X produce Control-C. It also makes Control-Alt-X produce Control-Alt-C, etc.</td>
  </tr>
  <tr>
    <td>RWin::Return</td>
    <td>Disables the right Windows key by having it simply <a href="../commands/Return.htm">return</a>.</td>
  </tr>
</table>
<p>You can try out any of these examples by copying them into a new text file such as &quot;Remap.ahk&quot;, then launching the file.</p>
<p>See the <a href="../KeyList.htm">Key List</a> for a complete list of key and mouse button names.</p>
<h2>Remarks</h2>
<p>The directives <a href="../commands/_IfWinActive.htm">#IfWinActive/Exist</a> can be used to make selected remappings active only in the windows you specify. For example:</p>
<pre>#IfWinActive ahk_class Notepad
a::b  <em>; Makes the 'a' key send a 'b' key, but only in Notepad.</em>
#IfWinActive  <em>; This puts subsequent remappings and hotkeys in effect for all windows.</em></pre>
<p>Remapping a key or button is &quot;complete&quot; in the following respects:</p>
<ul>
  <li>Holding down a modifier such as Control or Shift while typing the origin key will put that modifier into effect for the destination key. For example, <code>b::a</code> would produce Control-A if you press Control-B.</li>
  <li>Capslock generally affects remapped keys in the same way as normal keys.</li>
  <li>The destination key or button is held down for as long as you continue to hold down the origin key. However, some games do not support remapping; in such cases, the keyboard and mouse will behave as though not remapped.</li>
  <li>Remapped keys will auto-repeat while being held down (except keys remapped to become mouse buttons).</li>
</ul>
<p><a name="HookHotkeys"></a>Although a remapped key can trigger normal hotkeys, by default it cannot trigger mouse hotkeys or <a href="../commands/_UseHook.htm">hook hotkeys</a> (use <a href="../commands/ListHotkeys.htm">ListHotkeys</a> to discover which hotkeys are &quot;hook&quot;). For example, if the remapping <code>a::b</code> is in effect, pressing Ctrl-Alt-A would trigger the <code>^!b</code> hotkey only if <code>^!b</code> is not a hook hotkey. If <code>^!b</code> is a hook hotkey, you can define <code>^!a</code> as a hotkey if you want Ctrl-Alt-A to perform the same action as Ctrl-Alt-B. For example:</p>
<pre>a::b
^!a::
^!b::
ToolTip You pressed %A_ThisHotkey%.
return</pre>
<p>Alternatively, in v1.1.06 and later, <a href="../commands/_InputLevel.htm">#InputLevel</a> can be used to override the default behaviour. For example:</p>
<pre>#InputLevel 1
a::b

#InputLevel 0
^!b::
ToolTip You pressed %A_ThisHotkey%.
return</pre>
<p><a name="SendPlay"></a>If <a href="../commands/SendMode.htm">SendMode</a> is used in the auto-execute section (top part of the script), it affects all remappings. However, since remapping uses <a href="../commands/Send.htm#blind">Send {Blind}</a> and since the <a href="../commands/SendMode.htm">SendPlay mode</a> does not fully support  {Blind}, some remappings might not function properly in SendPlay mode (especially Control, Shift, Alt, and Win). To work around this, avoid SendPlay in auto-execute section when you have remappings; then use the command <a href="../commands/Send.htm#SendPlay">SendPlay</a> vs. Send in other places throughout the script. Alternatively, you could translate your remappings into hotkeys (as described below) that explicitly call SendEvent vs. Send.</p>
<p>When a script is launched, each remapping is translated into a pair of <a href="../Hotkeys.htm">hotkeys</a>. For example, a script containing <code>a::b</code> actually contains the following two hotkeys instead:</p>
<pre>*<strong>a</strong>::
SetKeyDelay -1   <em>; If the destination key is a mouse button, SetMouseDelay is used instead.</em>
Send <a href="../commands/Send.htm#blind">{Blind}</a>{<strong>b</strong> DownTemp}  <em>; DownTemp is like Down except that other Send commands in the script won't assume &quot;b&quot; should stay down during their Send.</em>
return

*<strong>a up</strong>::
SetKeyDelay -1  <em>; See note below for why press-duration is not specified with either of these SetKeyDelays.</em>
Send {Blind}{<strong>b</strong> Up}
return</pre>
<p>However, the above hotkeys vary under the following circumstances:</p>
<ol>
  <li>When the source key is LCtrl and the destination key is an Alt key, the line <code>Send {Blind}{LAlt DownTemp}</code> is replaced by <code>Send {Blind}<strong>{LCtrl Up}</strong>{LAlt DownTemp}</code>. The same is true if the source is RCtrl, except that <code>{RCtrl up}</code> is used.</li>
  <li>When a keyboard key is being remapped to become a mouse button (e.g. <code>RCtrl::RButton</code>), the hotkeys above use SetMouseDelay in place of SetKeyDelay. In addition, the first hotkey above is replaced by the following, which prevents the keyboard's auto-repeat feature from generating repeated mouse clicks:
    <pre>*RCtrl::
SetMouseDelay -1
if not GetKeyState(&quot;RButton&quot;)  <em>; i.e. the right mouse button isn't down yet.</em>
    Send {Blind}{RButton DownTemp}
return</pre>
  </li>
</ol>
<p>Note that SetKeyDelay's second parameter (<a href="../commands/SetKeyDelay.htm#dur">press duration</a>) is omitted in the hotkeys above. This is because press-duration does not apply to down-only or up-only events such as <code>{b down}</code> and <code>{b up}</code>. However, it does apply to changes in the state of the Shift/Ctrl/Alt/Win keys, which affects remappings such as <code>a::B</code> or <code>a::^b</code>. Consequently, any press-duration a script puts into effect via its <a href="../Scripts.htm#auto">auto-execute section</a> will apply to all such remappings.</p>
<p>Although a pair of keys cannot be directly remapped to single key (e.g. it's invalid to write <code>a &amp; c::b</code>), this effect can be achieved by explicitly adding the up and down hotkeys from the example higher above: simply replace <code>*a::</code> with <code>a &amp; c::</code>, and replace <code>*a up::</code> with <code>a &amp; c up::</code>.</p>
<p>Since remappings are translated into hotkeys as described above, the <a href="../commands/Suspend.htm">Suspend</a> command affects them. Similarly, the <a href="../commands/Hotkey.htm">Hotkey</a> command can disable or modify a remapping. For example, the following two commands would disable the remapping <code>a::b</code>.</p>
<pre>Hotkey, *a, off
Hotkey, *a up, off</pre>
<p><a name="AltTab"></a>Alt-tab issues: If you remap a key or mouse button to become an Alt key, that key will probably not be able to alt-tab properly. A possible work-around is to add the hotkey <code>*Tab::Send {Blind}{Tab}</code> -- but be aware that it will likely interfere with using the real Alt key to alt-tab. Therefore, it should be used only when you alt-tab solely by means of remapped keys and/or <a href="../Hotkeys.htm#alttab">alt-tab hotkeys</a>.</p>
<p>In addition to the keys and mouse buttons on the <a href="../KeyList.htm">Key List</a> page, the source key may also be a virtual key (VKnn) or scan code (SCnnn) as described on the <a href="../KeyList.htm#SpecialKeys">special keys</a> page. The same is true for the destination key except that it may optionally specify a scan code after the virtual key. For example, <code>sc01e::vk42sc030</code> is equivalent to <code>a::b</code> on most keyboard layouts.</p>
<p>To disable a key rather than remapping it, make it a hotkey that simply <a href="../commands/Return.htm">returns</a>. For example, <code>F1::return</code> would disable the F1 key.</p>
<p>The following keys are not supported by the built-in remapping method:</p>
<ul>
  <li>The mouse wheel (WheelUp/Down/Left/Right).</li>
  <li>Pause and Break as destination keys (since they match the names of commands).</li>
  <li>Curly braces {} as destination keys. Instead use the <a href="../commands/Send.htm#vk">VK/SC method</a>; e.g. <code>x::+sc01A</code> and <code>y::+sc01B</code>.</li>
  <li>A percent sign (%) as a destination key. Instead use the <a href="../commands/Send.htm#vk">VK/SC method</a>.</li>
  <li>&quot;Return&quot; as a destination key. Instead use &quot;Enter&quot;.</li>
</ul>
<h2>Moving the Mouse Cursor via the Keyboard</h2>
<p>The keyboard can be used to move the mouse cursor as demonstrated by the fully-featured <a href="../scripts/NumpadMouse.htm">Keyboard-To-Mouse script</a>. Since that script offers smooth cursor movement, acceleration, and other features, it is the recommended approach if you plan to do a lot of mousing with the keyboard. By contrast, the following example is a simpler demonstration:</p>
<pre>*#up::MouseMove, 0, -10, 0, R  <em>; Win+UpArrow hotkey =&gt; Move cursor upward</em>
*#Down::MouseMove, 0, 10, 0, R  <em>; Win+DownArrow =&gt; Move cursor downward</em>
*#Left::MouseMove, -10, 0, 0, R  <em>; Win+LeftArrow =&gt; Move cursor to the left</em>
*#Right::MouseMove, 10, 0, 0, R  <em>; Win+RightArrow =&gt; Move cursor to the right</em>

*&lt;#RCtrl::  <em>; LeftWin + RightControl =&gt; Left-click (hold down Control/Shift to Control-Click or Shift-Click).</em>
SendEvent {Blind}{LButton down}
KeyWait RCtrl  <em>; Prevents keyboard auto-repeat from repeating the mouse click.</em>
SendEvent {Blind}{LButton up}
return

*&lt;#AppsKey::  <em>; LeftWin + AppsKey =&gt; Right-click</em>
SendEvent {Blind}{RButton down}
KeyWait AppsKey  <em>; Prevents keyboard auto-repeat from repeating the mouse click.</em>
SendEvent {Blind}{RButton up}
return</pre>
<h2 id="registry">Remapping via the Registry's &quot;Scancode Map&quot;</h2>
<p><strong>Advantages:</strong></p>
<ul>
  <li>Registry remapping is generally more pure and effective than <a href="#Remap">AutoHotkey's remapping</a>. For example, it works in a broader variety of games, it has no known <a href="#AltTab">alt-tab issues</a>, and it is capable of firing AutoHotkey's hook hotkeys  (whereas AutoHotkey's remapping requires a <a href="#HookHotkeys">workaround</a>).</li>
  <li>If you choose to make the registry entries manually (explained below), absolutely no external software is needed to remap your keyboard. Even if you use <a href="http://webpages.charter.net/krumsick/">KeyTweak</a> to make the registry entries for you, KeyTweak does not need to stay running all the time (unlike AutoHotkey).</li>
</ul>
<p><strong>Disadvantages:</strong></p>
<ul>
  <li>Registry remapping is relatively permanent: a reboot is required to undo the changes or put new ones into effect.</li>
  <li>Its effect is global: it cannot create remappings specific to a particular user, application, or locale.</li>
  <li>It cannot send keystrokes that are modified by Shift, Control, Alt, or AltGr. For example, it cannot remap a lowercase character to an uppercase one.</li>
  <li>It supports only the keyboard (AutoHotkey has <a href="#RemapMouse">mouse remapping</a> and some <a href="RemapJoystick.htm">limited joystick remapping</a>).</li>
</ul>
<p><strong>How to Apply Changes to the Registry:</strong> There are at least two methods to remap keys via the registry:</p>
<ol>
  <li>Use a program like <a href="http://webpages.charter.net/krumsick/">KeyTweak</a> (freeware) to visually remap your keys. It will change the registry for you.</li>
  <li>Remap keys manually by creating a .reg file (plain text) and loading it into the registry. This is demonstrated at <a href="http://www.autohotkey.com/forum/post-56216.html#56216">www.autohotkey.com/forum/post-56216.html#56216</a></li>
</ol>
<h2>Related Topics</h2>
<p><a href="../KeyList.htm#Joystick">List of keys and mouse buttons</a><br>
<a href="../commands/GetKeyState.htm">GetKeyState</a><br>
<a href="RemapJoystick.htm">Remapping a joystick</a></p>
</body>
</html>
